"""
诊断路由注册问题
"""
import sys

print("="*60)
print("诊断路由注册问题")
print("="*60)

# 1. 测试导入
print("\n1️⃣  测试导入 app.api.product...")
try:
    from app.api import product
    print("✅ 导入成功")
    print(f"   router对象: {product.router}")
except Exception as e:
    print(f"❌ 导入失败: {e}")
    import traceback
    traceback.print_exc()
    sys.exit(1)

# 2. 检查路由
print("\n2️⃣  检查路由定义...")
routes = product.router.routes
print(f"✅ 共有 {len(routes)} 个路由:")
for route in routes:
    if hasattr(route, 'path') and hasattr(route, 'methods'):
        print(f"   {list(route.methods)[0] if route.methods else 'N/A'} {route.path}")

# 3. 检查价格历史路由
print("\n3️⃣  检查价格历史路由...")
price_history_route = None
for route in routes:
    if hasattr(route, 'path') and '/price-history' in route.path:
        price_history_route = route
        break

if price_history_route:
    print("✅ 找到价格历史路由")
    print(f"   路径: {price_history_route.path}")
    print(f"   方法: {price_history_route.methods}")
else:
    print("❌ 未找到价格历史路由")

# 4. 测试导入 get_price_history
print("\n4️⃣  测试导入 get_price_history 函数...")
try:
    from app.services.tools import get_price_history
    print("✅ 导入成功")
    print(f"   函数: {get_price_history}")
except Exception as e:
    print(f"❌ 导入失败: {e}")
    import traceback
    traceback.print_exc()

# 5. 测试完整应用
print("\n5️⃣  测试应用路由...")
try:
    from app.main import app
    print("✅ 应用加载成功")
    
    all_routes = []
    for route in app.routes:
        if hasattr(route, 'path'):
            all_routes.append(route.path)
    
    print(f"\n所有路由 ({len(all_routes)} 个):")
    product_routes = [r for r in all_routes if '/products/' in r]
    if product_routes:
        print("  商品相关路由:")
        for r in sorted(product_routes):
            print(f"    {r}")
    else:
        print("  ❌ 没有找到商品相关路由")
    
    # 特别检查价格历史路由
    price_history_in_app = any('/price-history' in r for r in all_routes)
    if price_history_in_app:
        print("\n✅ 应用中包含价格历史路由")
    else:
        print("\n❌ 应用中不包含价格历史路由")
        
except Exception as e:
    print(f"❌ 应用加载失败: {e}")
    import traceback
    traceback.print_exc()

print("\n" + "="*60)
print("诊断完成")
print("="*60)

